package com.avertymaroc.library;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.avertymaroc.app.R;
import com.avertymaroc.beans.Option;
import com.avertymaroc.beans.SondageRapide;

public class ELVAdapter extends BaseExpandableListAdapter {

	private Context context;

	private ArrayList<SondageRapide> sondageListe;

	private LayoutInflater inflater;

	public ELVAdapter(Context context, ArrayList<SondageRapide> list) {

		this.context = context;

		this.sondageListe = list;

		inflater = LayoutInflater.from(context);

	}

	@Override
	public boolean areAllItemsEnabled() {

		return true;

	}

	public Object getChild(int gPosition, int cPosition) {

		return sondageListe.get(gPosition);

	}

	public long getChildId(int gPosition, int cPosition) {

		return cPosition;

	}

	
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		 SondageRapide sondage = sondageListe.get(groupPosition);

		ListOptions listOptions ;
		
		if (convertView == null) {
			convertView = inflater
					.inflate(R.layout.gr_sond_rap_child, null);
			 listOptions = new ListOptions(convertView);
			convertView.setTag(listOptions);
		}else{
			listOptions = (ListOptions)convertView.getTag();
			}
		
		listOptions.drawLayout(sondage.getOptions(), sondage.isMulti());

		return convertView;
	}

	public int getChildrenCount(int gPosition) {

		return 1;

	}

	public Object getGroup(int gPosition) {

		return sondageListe.get(gPosition);

	}

	public int getGroupCount() {

		return sondageListe.size();

	}

	public long getGroupId(int gPosition) {

		return gPosition;

	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		SondageRapide sondage = sondageListe.get(groupPosition);

		if (convertView == null)
			convertView = inflater.inflate(R.layout.group_row, null);

		TextView titre = (TextView) convertView.findViewById(R.id.TVGroup);

		titre.setText(sondage.getTitre());

		return convertView;

	}

	public boolean hasStableIds() {

		return true;

	}

	public boolean isChildSelectable(int arg0, int arg1) {

		return true;

	}

	
	
	class ListOptions {

		public RadioGroup choiceGroup;
		// public LinearLayout groupView;
		LinearLayout layout;

		RadioGroup.LayoutParams params;
		public ListOptions( View v ) {
			layout = (LinearLayout) v.findViewById(R.id.sond_rap_elt);
			params = new RadioGroup.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			params.setMargins(5, 0, 5, 5);
			
		}


		public void drawLayout(Option[] options , boolean isMulti) {
			choiceGroup = new RadioGroup(context);
			if (isMulti){
				for (Option opt : options) {
					CheckBox c = new CheckBox(context);
					c.setText(opt.getTitre());
					c.setBackgroundColor(Color.parseColor("#6f6f6f"));

					choiceGroup.addView(c, params);
				}
			}
			else{
				for (Option opt : options) {
					RadioButton r = new RadioButton(context);
					r.setText(opt.getTitre());
					r.setBackgroundColor(Color.parseColor("#6f6f6f"));
					choiceGroup.addView(r, params);
				}
			}
			layout.removeAllViewsInLayout();
			layout.addView(choiceGroup, new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.MATCH_PARENT));
		}
	}

	class UniqueChoice {

		public RadioGroup choiceGroup;

		LinearLayout layout;

		public UniqueChoice(Option[] options, View v) {

			choiceGroup = new RadioGroup(context);
			layout = (LinearLayout) v.findViewById(R.id.sond_rap_elt);

			for (Option opt : options) {

				RadioButton r = new RadioButton(context);
				r.setText(opt.getTitre());

				r.setBackgroundColor(Color.parseColor("#6f6f6f"));

				RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
						ViewGroup.LayoutParams.MATCH_PARENT,
						ViewGroup.LayoutParams.WRAP_CONTENT);
				params.setMargins(5, 0, 5, 5);

				choiceGroup.addView(r, params);
			}
		}

		public void drawLayout() {
			layout.removeAllViews();
			layout.addView(choiceGroup, new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.MATCH_PARENT));
		}
	}

	class MultiplChoice {
		public LinearLayout layout;

		public LinearLayout groupView;

		public MultiplChoice(Option[] options, View v) {
			layout = (LinearLayout) v.findViewById(R.id.sond_rap_elt);

			groupView = new LinearLayout(context);
			groupView.setOrientation(LinearLayout.VERTICAL);

			for (Option opt : options) {

				CheckBox c = new CheckBox(context);

				c.setText(opt.getTitre());
				c.setBackgroundColor(Color.parseColor("#6f6f6f"));

				RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
						ViewGroup.LayoutParams.MATCH_PARENT,
						ViewGroup.LayoutParams.WRAP_CONTENT);
				params.setMargins(5, 0, 5, 5);

				groupView.addView(c, params);
			}
		}

		public void drawLayout() {
			layout.removeAllViews();
			layout.addView(groupView, new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.MATCH_PARENT));
		}

	}

	/*
	 * private void afficheerResultat(Option[] opts, LinearLayout options,
	 * SondageRapide sondage) {
	 * 
	 * if (opts == null) opts = sondage.getOptions();
	 * 
	 * LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
	 * ViewGroup.LayoutParams.MATCH_PARENT,
	 * ViewGroup.LayoutParams.WRAP_CONTENT); params.setMargins(5, 0, 3, 5);
	 * 
	 * options.removeAllViews();
	 * 
	 * for (Option opt : opts) {
	 * 
	 * PercentBar bar = new PercentBar(SondageRapDetails.this);
	 * bar.setBackgroundResource(R.drawable.option_bg); bar.setText("( " +
	 * opt.getPercent() + "% ) " + opt.getTitre());
	 * bar.setPercent(opt.getPercent());
	 * bar.setTextColor(Color.parseColor("#FFFFFF")); bar.setPadding(2, 3, 2,
	 * 2); options.addView(bar, params);
	 * 
	 * }
	 * 
	 * }
	 */

}
